#include "confighelper.h"
#include <QFile>

//配置的选项
static const QString STOCKS("stocks");
static const QString STOCKS_NAME("stocks_name");
static const QString ENABLE_PROXY("enable_proxy");
static const QString HTTP_PROXY("http_proxy");
static const QString PROXY_PORT("proxy_port");
static const QString INTERVAL("interval");
static const QString WINDOW("window");
static const QString NOT_ON_TOOLBAR("not_on_toolbar");

//默认的股票
static const QString DEFAULT_STOCK("sh601857,sz000001");


ConfigHelper::ConfigHelper(QString file) :
        configFile(file)
{
    //如果配置文件不存在 创建一个默认配置
    QFile conFile(configFile);
    if(conFile.exists() == false){
        setting_ = new QSettings(configFile, QSettings::IniFormat);
        setting_->setValue(STOCKS, DEFAULT_STOCK);
        setting_->setValue(ENABLE_PROXY, false);
        setting_->setValue(HTTP_PROXY, "");
        setting_->setValue(PROXY_PORT, "");
        setting_->setValue(INTERVAL, "5000");
        setting_->setValue(WINDOW, 0);
        setting_->setValue(NOT_ON_TOOLBAR, false);
    } else {
        setting_ = new QSettings(configFile, QSettings::IniFormat);
    }
}

ConfigHelper::~ConfigHelper()
{
    delete setting_;
}

QString ConfigHelper::getStocksStr()
{
    return setting_->value(STOCKS, DEFAULT_STOCK).toString();
}

void ConfigHelper::setStocksStr(QString str)
{
    setting_->setValue(STOCKS, str);
}
void ConfigHelper::setStocksNameStr(QString str)
{
    setting_->setValue(STOCKS_NAME, str);
}

bool ConfigHelper::isProxyEnable()
{
    return setting_->value(ENABLE_PROXY, false).toBool();
}

void ConfigHelper::setProxyEnable(bool enable)
{
    setting_->setValue(ENABLE_PROXY, enable);
}

void ConfigHelper::setProxy(QString url)
{
    setting_->setValue(HTTP_PROXY, url);
}

void ConfigHelper::setProxyPort(QString port)
{
    setting_->setValue(PROXY_PORT, port);
}

QString ConfigHelper::getProxy()
{
    return setting_->value(HTTP_PROXY).toString();
}

int ConfigHelper::getProxyPort()
{
    return setting_->value(PROXY_PORT).toInt();
}

int ConfigHelper::getInterval()
{
    return setting_->value(INTERVAL, 5000).toInt();
}

void ConfigHelper::reloadConfig()
{
    delete setting_;
    QFile conFile(configFile);
    if(conFile.exists() == false){
        setting_ = new QSettings(configFile, QSettings::IniFormat);
        setting_->setValue(STOCKS, DEFAULT_STOCK);
        setting_->setValue(ENABLE_PROXY, false);
        setting_->setValue(HTTP_PROXY, "");
        setting_->setValue(PROXY_PORT, "");
        setting_->setValue(INTERVAL, "5000");
        setting_->setValue(WINDOW, 0);
        setting_->setValue(NOT_ON_TOOLBAR, false);
    }
    else
        setting_ = new QSettings(configFile, QSettings::IniFormat);
}

void  ConfigHelper::setHasnew(bool hasNew)
{
    hasNew_ = hasNew;
}

bool ConfigHelper::getHasnew()
{
    return hasNew_;
}

int ConfigHelper::getWindowStat()
{
    return setting_->value(WINDOW,0).toInt();
}

void ConfigHelper::setWindowStat(int value)
{
    setting_->setValue(WINDOW,value);
}

bool ConfigHelper::isNotOnToolbar()
{
    return setting_->value(NOT_ON_TOOLBAR, false).toBool();
}

void ConfigHelper::setNotOnToolbar(bool value)
{
    setting_->setValue(NOT_ON_TOOLBAR,value);
}
